/**
 */
package supermemo.input;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


/**
 * 
 * @author <a href='mailto:Yan-Qi@hotmail.com'>Yan Qi</a>
 * @version $Revision$
 * @latest $Id$
 */
public final class UtilCenter
{
	public static Vector<String> ParseMultiInput(final String pInputString, final String pDelimit)
	{
		Vector<String> ret = new Vector<String>();
		
		try {
			Pattern Regex = Pattern.compile(pDelimit,Pattern.CANON_EQ);
			Matcher RegexMatcher = Regex.matcher(pInputString);
			while (RegexMatcher.find()) 
			{
				if(!RegexMatcher.group().trim().equals(""))
				{
					ret.add(RegexMatcher.group());
				}
			} 
		} catch (PatternSyntaxException ex) 
		{
			ex.printStackTrace();
		}
//		System.out.println("The list of string is:"+ret.toString());
		return ret;
	}
	
	public static Vector<Integer> ParseMultiInputInteger(final String pInputString, final String pDelimit)
	{
		Vector<String> string_list = ParseMultiInput(pInputString, pDelimit);
		Vector<Integer> ret = new Vector<Integer>();
		for(String cur_str : string_list)
		{
			try
			{
				ret.add(Integer.parseInt(cur_str));
			} catch(NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		
//		System.out.println("The list of integer is:"+ret.toString());
		return ret;
	}
	
	public static boolean ExtractStringElements(final String pInputString, final String pDelimit, Collection<String> ret)
	{
		try {
			Pattern Regex = Pattern.compile(pDelimit,Pattern.CANON_EQ);
			Matcher RegexMatcher = Regex.matcher(pInputString);
			while (RegexMatcher.find()) 
			{
				if(!RegexMatcher.group().trim().equals(""))
				{
					ret.add(RegexMatcher.group());
				}
			} 
		} catch (PatternSyntaxException ex) 
		{
			ex.printStackTrace();
		}
//		System.out.println("The list of string is:"+ret.toString());
		return true;
	}
	
	public static boolean ExtractIntegerElements(final String pInputString, final String pDelimit, Collection<Integer> ret)
	{
		Vector<String> string_list = ParseMultiInput(pInputString, pDelimit);
		//System.out.println(string_list.size());
		for(String cur_str : string_list)
		{
			try
			{
				ret.add(Integer.parseInt(cur_str));
			} catch(NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
//		System.out.println("The list of integer is:"+ret.toString());
		return true;
	}

	static public String ObjectArrayToStringList(final Object[] objArray, final String delimit)
	{
		StringBuffer val_list = new StringBuffer();
		int number_of_values = objArray.length;
		for(int i=0; i<number_of_values; ++i)
		{
			val_list.append((String)objArray[i]);
			if(number_of_values > 1 && i < number_of_values-1 )
			{
				val_list.append(delimit);
			}
		}
		return val_list.toString();
	}
	
	/**
	 * Description check if the string "input" can be converted into one integer.
	 * 
	 * @param input
	 * @return
	 */
	static public boolean IsInteger(final String input)
	{
		try{
			Integer.parseInt(input);
			return true; //Did not throw, must be a number
		}catch(NumberFormatException err){
			return false; //Threw, So is not a number
		}
	}
	
	
	/**
	 * The method is cited from: http://blog.csdn.net/dyhml/archive/2005/02/17/291158.aspx.
	 * @param window
	 */
	public static void Move2ScreenCenter(Window window)
	{
		 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		 Dimension windowSize = window.getSize();
		 if (windowSize.height > screenSize.height)
		      windowSize.height = screenSize.height;
		 if (windowSize.width > screenSize.width)
		      windowSize.width = screenSize.width;
		 window.setLocation((screenSize.width-windowSize.width)/2,(screenSize.height-windowSize.height)/2);
	}
	
	
	/**
	 * Calculate the intersection of two sets
	 * @param s1
	 * @param s2
	 * @return
	 */
	@SuppressWarnings("unchecked")
	static public Set get_intersection_of_sets(Set s1, Set s2)
	{
		Set ret = new HashSet();
		ret.addAll(s1);
		ret.retainAll(s2);
		return ret;
	}
	
	/**
	 * Calculate the intersection of sets
	 * @param sets
	 * @return
	 */
	@SuppressWarnings("unchecked")
	static public Set get_intersection_of_sets(Collection<Set> sets)
	{
		Set intersection = new HashSet();
		boolean is_first_set = true;
		
		for(Set cur_set : sets)
		{
			if(is_first_set)
			{
				is_first_set = false;
				intersection.addAll(cur_set);
				continue;
			}
			intersection.retainAll(cur_set);
		}
		
		return intersection;
	}
}
